/**
 *
 *
 * 带千分符，保留2位小数
 * 
 * 
 */
export function toThousandDecimalFilter (num) {
  let DataArry = [];
  let string = '';
  let negativeValue = false;
  let floatPart = "";
  let intPart = "";
  if (num == 0) {
    string = '0.00'
    return string;
  }
  if (num) {
    num = num.toString();
    if (num.charAt(0) == '-') {
      negativeValue = true;
    }

    num = num.replace(/[^0-9.]/g, '');
    DataArry = num.split(".");
    if (DataArry[0]) {
      if (DataArry[0].length > 1) {
        let intPartArry = DataArry[0];
        let intPartArryIndex = 0;
        let isZero = true;
        for (let i in intPartArry) {
          if (intPartArry.charAt(i) != 0) {
            intPartArryIndex = i;
            isZero = false
            break;
          }
        }
        if (isZero) {
          intPart = '0'
        } else {
          intPart = intPartArry.substring(intPartArryIndex)
        }

      } else {
        intPart = DataArry[0]
      }
    } else {
      intPart = '0'
    }
    intPart = intPart.replace(/(\d)(?=(?:\d{3})+$)/g, "$1,")
    if (DataArry[1]) {
      switch (DataArry[1].length) {
        case 0:
          floatPart = "00";
          break;
        case 1:
          floatPart = DataArry[1] + "0";
          break;
        default:
          floatPart = DataArry[1].substring(0, 2)
          break;
      }
    } else {
      floatPart = "00";
    }
    string = intPart + '.' + floatPart;
    if (negativeValue == true) {
      string = '-' + string
    }
  }
  return string;
}

/**
 *
 *
 * 去除千分符，保留2位小数
 * 
 * 
 */
export function removeThousandDecimalFilter (num) {
  let DataArry = [];
  let string = '';
  let negativeValue = false;
  let floatPart = "";
  let intPart = "";
  if (num) {

    num = num.toString();

    if (num.charAt(0) == '-') {
      negativeValue = true;
    }
    num = num.replace(/[^0-9.]/g, '');
    DataArry = num.split(".");
    if (DataArry[0]) {
      if (DataArry[0].length > 1) {
        let intPartArry = DataArry[0];
        let intPartArryIndex = 0;
        let isZero = true;
        for (let i in intPartArry) {
          if (intPartArry.charAt(i) != 0) {
            intPartArryIndex = i;
            isZero = false
            break;
          }
        }
        if (isZero) {
          intPart = '0'
        } else {
          intPart = intPartArry.substring(intPartArryIndex)
        }

      } else {
        intPart = DataArry[0]
      }
    } else {
      intPart = '0'
    }
    if (DataArry[1]) {
      switch (DataArry[1].length) {
        case 0:
          floatPart = "00";
          break;
        case 1:
          floatPart = DataArry[1] + "0";
          break;
        default:
          floatPart = DataArry[1].substring(0, 2)
          break;
      }
    } else {
      floatPart = "00";
    }
    string = intPart + '.' + floatPart;

    if (negativeValue == true) {
      string = '-' + string
    }
  }
  return string;
}


/**
 *
 *
 * 生成树结构
 * 
 * 
 */
export function differentiateTree (arr, rule = { parentId: "nodeParentId", id: "nodeId", startParentId: 0 }) {
  let arrcloned = [...arr]
  const handle = id => {
    let arr = []
    arrcloned.forEach(folder => {
      if (folder[rule.parentId] == id) {
        const children = handle(folder[rule.id])

        if (folder.children) {
          folder.children = [].concat(folder.children, children)
        }
        else {
          if (children.length == 0) {

          } else {
            folder.children = children
          }

        }
        // folder.title = folder.name
        arr.push(folder)
      }
    })
    return arr
  }
  return handle(rule.startParentId)
}


/**
 *
 *
 * 树结构扁平化
 * 
 * 
 */
export function treeFlat (source) {
  let temData = []
  function deeploop (source) {
    source.forEach(el => {
      temData.push(el)
      el.children && el.children.length > 0 ? deeploop(el.children) : ""        // 子级递归
    })
  }
  deeploop(source)
  return temData
}
